using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CWD;
using System.Linq;
using CWD.BLL;


namespace CWD
{
    public partial class UserAuth : Form
    {
        public ModelUserBLL muBll;
        public UserAuth()
        {
            InitializeComponent();
        }

        private void UserAuth_Load(object sender, EventArgs e)
        {
            muBll = new ModelUserBLL();            
            label1.BackColor = Color.Transparent;
            label2.BackColor = Color.Transparent;
        }

        private void tbUser_KeyDown(object sender, KeyEventArgs e)
        {
            tbUser.Text = "";
            InputNum input = new InputNum();
            input.Left = 1000;
            //input.StartPosition =new Point(100,200);
            if (input.ShowDialog() != DialogResult.OK)
                return;
            PersonBLL perBll = new PersonBLL();
            var rlt = perBll.GetQuery().Where(p => p.cPsn_Num == input.cResult).Select(p => new { p.cPsn_Name }).ToList();            
            if (rlt.Count > 0)
            {
                tbUser.Text = rlt[0].cPsn_Name;
                tbUser.Value = input.cResult;
            }
        }
        

        private void tbPassword_Click(object sender, EventArgs e)
        {
            tbPassword.Text = "";
            InputNum input = new InputNum();
            if (input.ShowDialog() != DialogResult.OK)
                return;
            string modelname = Public.getProgramName();
            string password = Public.GetMd5Str(input.cResult);
            var rlt = muBll.GetQuery().Where(p => p.Model == modelname && p.cPsn_Num == tbUser.Value).ToList();
            if (rlt.Count > 0)
            {
                CWD.EF.C_ModelUser user = muBll.GetEntityByID(rlt[0].ID);
                if (user != null && user.PassWord==password)
                {
                    Public.userName = tbUser.Text;
                    Public.userCode = tbUser.Value;
                    if(user.Oper=="Audit")
                        Public.bAudit = true;
                    this.DialogResult = DialogResult.OK;
                    this.Dispose();
                }
            }           
        }

        private void tbUser_Click(object sender, EventArgs e)
        {
            tbUser.Text = "";
            InputNum input = new InputNum();
            if (input.ShowDialog() != DialogResult.OK)
                return;
            var rlt =new PersonBLL().GetQuery().Where(p => p.cPsn_Num == input.cResult).Select(p=>new {p.cPsn_Name}).ToList();
            if (rlt.Count>0)
            {
                tbUser.Text =rlt[0].cPsn_Name;
                tbUser.Value = input.cResult;
            }
            btnModi.Visible = true;
            if (tbUser.Value == "00019")
            {
                btnRegister.Visible = true;
            }
        }

        private void btnModi_Click(object sender, EventArgs e)
        {
            fmPassword Newpass = new fmPassword();
            Newpass.UserID = tbUser.Value;
            //Newpass.ufconn = ufconn;
            Newpass.Show();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            fmRegister register = new fmRegister();
            //register.ufconn = ufconn;
            register.Show();
        }

        

    }
}